package com.link2room.blaster.service.impl;

import com.link2room.blaster.service.InmailReceiverService;
import com.link2room.common.dao.ICommonDao;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.blaster.entity.InMailReceiverEntity;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by LN on 2017/6/29.15:06
 */
@Component("inmailReceiverService")
public class InmailReceiverServiceImpl implements InmailReceiverService {
    @Resource
    private ICommonDao commonDao;
    @Override
    @Transactional
    public void updateInmailReceiver(String lhotelGroupCode,String lhotelCode , String type ,List<String> toDel, List<String> toSave) throws Exception {
        if(CollectionUtils.isNotEmpty(toDel)){
            L2RCondition delCondition = new L2RCondition();
            delCondition.setAndConditions(
                    new ConditionDetail[]{
                            new ConditionDetail(InMailReceiverEntity.InMailReceiverEntity_.receiverCode.toString(), CriteriaUtil.Operator.IN, toDel),
                            new ConditionDetail(InMailReceiverEntity.InMailReceiverEntity_.inmailType.toString(), CriteriaUtil.Operator.EQ, type),
                            new ConditionDetail(InMailReceiverEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ, lhotelGroupCode),
                            new ConditionDetail(InMailReceiverEntity.BaseEntity_.lhotelCode.toString(), CriteriaUtil.Operator.EQ, lhotelCode)
                    }
            );
            commonDao.delete(InMailReceiverEntity.class, CriteriaUtil.getCriteria(delCondition));
        }
        if(CollectionUtils.isNotEmpty(toSave)){
            List<InMailReceiverEntity> saves = new ArrayList<>();
            toSave.stream()
                    .forEach(code -> {
                        InMailReceiverEntity newEntity = new InMailReceiverEntity();
                        newEntity.setLhotelGroupCode(lhotelGroupCode);
                        newEntity.setLhotelCode(lhotelCode);
                        newEntity.setInmailType(type);
                        newEntity.setReceiverCode(code);
                        saves.add(newEntity);
                    });
            commonDao.saveList(saves);
        }
    }

    @Override
    public List<String> findReceiver(String lhotelGroupCode, String lhotelCode, String type) throws Exception {
        InMailReceiverEntity find = new InMailReceiverEntity();
        find.setLhotelCode(lhotelCode);
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setInmailType(type);
        QueryResult<InMailReceiverEntity> qres = commonDao.findAll(InMailReceiverEntity.class, CriteriaUtil.getCriteriaAllEQ(find), null, null, null, null);
        if(qres == null || CollectionUtils.isEmpty(qres.getResultlist())){
            return Collections.emptyList();
        }else {
            return qres.getResultlist().stream()
                    .map(entity->entity.getReceiverCode())
                    .distinct()
                    .collect(Collectors.toList());
        }
    }
}
